﻿using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using AzureTracer.ViewModels;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Documents;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using Serialization;
using AzureTracer.Model;

namespace AzureTracer.Views
{
    /// <summary>
    /// Description for DetailPage.
    /// </summary>
    ///
    public partial class DetailPage : PhoneApplicationPage
    {

        bool _isNewPageInstance = false;

        /// <summary>
        /// Initializes a new instance of the DetailPage class.
        /// </summary>
        public DetailPage()
        {
            InitializeComponent();
            this.Loaded += OnLoaded;
            _isNewPageInstance = true;
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            ApplicationBar.MatchOverriddenTheme();
            ApplicationBar.BackgroundColor = Colors.White;
        }


        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            if (e.NavigationMode != NavigationMode.Back)
                State["ViewModel"] = SilverlightSerializer.Serialize(this.DataContext);            
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {           
            if (!_isNewPageInstance) return;

            if (State.Count != 0)
                DataContext = SilverlightSerializer.Deserialize(State["ViewModel"] as byte[]);                                       
            State.Clear();

            _isNewPageInstance = false;
        }
    }
}